package com.syni.mdd.sjb.common.datatreaure.handler.news;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppNews;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppUserChannel;
import com.syni.mdd.sjb.common.component.utils.common.spring.ApplicationContextHolder;
import com.syni.mdd.sjb.common.component.utils.common.utils.RequestHeaderData;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.dataBankBusiness.NewDetailParamsRpVo;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppUserChannelService;

import java.util.Map;

public class OtherNewsHandler extends NewsHandler {
    @Override
    public void handle(DxshAppNews dxshAppNews, NewDetailParamsRpVo newDetailParamsRpVo) {
        DxshAppUserChannelService dxshAppUserChannelService = ApplicationContextHolder.getBean(DxshAppUserChannelService.class);
        String minAppIndex = dxshAppUserChannelService.getMinAppIndexUrl();
        RequestHeaderData requestHeaderData = new RequestHeaderData();
        newDetailParamsRpVo.setStoreId(dxshAppNews.getStoreId());
        if(dxshAppNews.getMessageSecondType() == Constants.DXSH_APP_NEW_ORTHER_MDD){
            //这里有好多类型
            Map<String,Object> dataMap = JSON.parseObject(dxshAppNews.getContent());
            Integer parentType = Integer.parseInt(dataMap.get("parentType").toString());
            Integer childType = Integer.parseInt(dataMap.get("childType").toString());
            Map<String,Object> detailContent = JSON.parseObject(dataMap.get("content").toString());
            if(parentType == Constants.SYSTEM_MESSAGE){
                if(childType == Constants.SYSTEM_MESSAGE_BUSINE_BUSINESS_AUDIT){
                    Integer status = Integer.parseInt(dataMap.get("status").toString());
                    if(status == Constants.COMMON_FLAG_TRUE_INT){
                        newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.BUSINESS_AUDIT_SUCCESS);
                    }else {
                        newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.BUSINESS_AUDIT_FAIL);
                    }
                    newDetailParamsRpVo.setBusinessId(Integer.parseInt(dataMap.get("bmsBusinessId").toString()));
                }else if(childType == Constants.SMBUSINE_PAYID_CONFIRM){
                    newDetailParamsRpVo.setBusinessId(Integer.parseInt(dataMap.get("bmsBusinessId").toString()));
                    newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.BUSINESS_PAID_CONFIRM);
                    newDetailParamsRpVo.setStatus(Constants.COMMON_FLAG_FALSE_INT);
                    if(detailContent.containsKey("status")) {
                        newDetailParamsRpVo.setStatus(Integer.parseInt(detailContent.get("status").toString()));
                    }
                }else if(childType == Constants.SYSTEM_MESSAGE_BUSINE_BUSINESS_ERROR){
                    newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.BUSINESS_ERROR);
                }else if(childType == Constants.SYSTEM_MESSAGE_BUSINE_GROUP_BUY_AUDIT){
                    Integer status = Integer.parseInt(dataMap.get("status").toString());
                    Integer businessId = Integer.parseInt(dataMap.get("bmsBusinessId").toString());
                    newDetailParamsRpVo.setBusinessId(businessId);
                    if(status == Constants.COMMON_FLAG_TRUE_INT){
                        newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.BUSINESS_GROUP_AUDIT_SUCCESS);
                    }else {
                        newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.BUSINESS_GROUP_AUDIT_FAIL);
                    }
                    if(detailContent.containsKey("groupBuyId")) {
                        newDetailParamsRpVo.setMddGroupBuyId(Integer.parseInt(detailContent.get("groupBuyId").toString()));
                    }
                }else if(childType == Constants.SMBUSINE_GROUP_BUY_FORCED_OFFLINE){
                    newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.BUSINESS_GROUP_FORCED_OFFLINE);
                    Integer businessId = Integer.parseInt(dataMap.get("bmsBusinessId").toString());
                    newDetailParamsRpVo.setBusinessId(businessId);
                    if(detailContent.containsKey("groupBuyId")) {
                        newDetailParamsRpVo.setMddGroupBuyId(Integer.parseInt(detailContent.get("groupBuyId").toString()));
                    }
                }else if(childType == Constants.SYSTEM_MESSAGE_BUSINESS_OFFLINE){
                    newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.BUSINESS_OFFLINE);
                }else if(childType == Constants.SMBUSINE_GROUP_MESSAGE_FORCE_DELETE){
                    newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.BUSINESS_MESSAGE_GROUP_DELETE);
                }
            }

        }else if(dxshAppNews.getMessageSecondType() == Constants.DXSH_APP_NEW_ORTHER_MINAPP){
            newDetailParamsRpVo.setStoreId(dxshAppNews.getStoreId());
            String token = requestHeaderData.getString("token");
            //这里有好多类型
            String desc = dxshAppNews.getDescribes();
            if(StringUtils.isNotBlank(minAppIndex)) {
                newDetailParamsRpVo.setJumpUrl(minAppIndex);
                newDetailParamsRpVo.setJumpUrl(String.format("%s?userToken=%s",minAppIndex,token));
            }
            Map<String,Object> dataMap = JSON.parseObject(dxshAppNews.getContent());
            if(dataMap.containsKey("appStatus") && dataMap.containsKey("wxReleaseStatus")){
                Integer appStatus = Integer.parseInt(dataMap.get("appStatus").toString());
                String wxReleaseStatus = dataMap.get("wxReleaseStatus").toString();
                Integer wxReleaseStep = Integer.parseInt(dataMap.get("wxReleaseStep").toString());
                if(appStatus == Constants.MINAPP_STATUS_QUAR_ERROR){
                    newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.ORTHER_MES_QUAL_ERROR);
                }else if(appStatus == Constants.MINAPP_STATUS_CREATE_ING && wxReleaseStatus.equals("fail")){
                    newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.ORTHER_MES_BASE_ERROR);
                    if(wxReleaseStep == 2){
                        newDetailParamsRpVo.setBaseDataErrorTip(NewDetailParamsRpVo.BASE_ERROR_TIP_NAME);
                    }else if(wxReleaseStep == 3){
                        newDetailParamsRpVo.setBaseDataErrorTip(NewDetailParamsRpVo.BASE_ERROR_TIP_HEAD);
                    }if(wxReleaseStep == 4){
                        newDetailParamsRpVo.setBaseDataErrorTip(NewDetailParamsRpVo.BASE_ERROR_TIP_INTROD);
                    }
                    DxshAppUserChannel dxshAppUserChannel = dxshAppUserChannelService.getByStoreId(dxshAppNews.getStoreId());
                    if(dxshAppUserChannel != null){
                        newDetailParamsRpVo.setChannelId(dxshAppUserChannel.getId());
                    }
                }else if((appStatus == Constants.MINAPP_STATUS_CREATE_ING && wxReleaseStatus.equals("success")
                        && wxReleaseStep == 9) || appStatus == Constants.MINAPP_STATUS_CREATE_SUCCESS){
                    newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.ORTHER_MES_RELEASE_SUCCESS);
                }else if(appStatus == Constants.MINAPP_STATUS_CREATE_SUCCESS){
                    newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.ORTHER_MES_RELEASE_SUCCESS);
                }
            }else if("运费充值提醒".equals(desc)){
                newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.DEFAULT);
                if(dataMap.containsKey("jumpTo")){
                    String jumpToUrl = dataMap.get("jumpTo").toString();
                    newDetailParamsRpVo.setJumpUrl(String.format("%s&userToken=%s",jumpToUrl,token));
                    //newDetailParamsRpVo.setAction(NewDetailParamsRpVo.JUMP_ACTION);
                }
            }
        }else if(dxshAppNews.getMessageSecondType().equals(Constants.COMMON_FLAG_FALSE_INT)){
            Map<String,Object> dataMap = JSON.parseObject(dxshAppNews.getContent());
            //TODO 小程序类型通知
            if(dataMap.containsKey("newType")) {
                String newType = dataMap.get("newType").toString();
                if (newType.equals("groupDividedConfirm")) {
                    newDetailParamsRpVo.setGroupData(dxshAppNews.getContent());
                    newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.GROUP_DIVIDED_CONFIRM_CODE);
                }else if (newType.equals("groupRejectCancel")) {
                    newDetailParamsRpVo.setGroupData(dxshAppNews.getContent());
                    newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.GROUP_REJECT_CANCEL_CODE);
                }else if (newType.equals("groupCancelSuccess")) {
                    newDetailParamsRpVo.setGroupData(dxshAppNews.getContent());
                    newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.GROUP_CANCEL_SUCCESS_CODE);
                }else if (newType.equals("groupCancelApply")) {
                    newDetailParamsRpVo.setGroupData(dxshAppNews.getContent());
                    newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.GROUP_CANCEL_APPLY_CODE);
                }else if (newType.equals("groupBindSuccess")) {
                    newDetailParamsRpVo.setGroupData(dxshAppNews.getContent());
                    newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.GROUP_BIND_SUCCESS_CODE);
                }
            }
        }else if(dxshAppNews.getMessageSecondType() == Constants.DXSH_APP_NEW_ORTHER_SYS){
            Map<String,Object> dataMap = JSON.parseObject(dxshAppNews.getContent());
            if(dataMap.containsKey("newType")) {
                String newType = dataMap.get("newType").toString();
                //服务购买提醒
                if (newType.equals("servicePurchaseRemind")) {
                    Integer serviceId = Integer.parseInt(dataMap.get("serviceId").toString());
                    newDetailParamsRpVo.setMddDataBankServiceId(serviceId);
                    newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.ORTHER_MES_SERVICE_PUR_RE);
                } else if (newType.equals("smsPurchase")) {
                    newDetailParamsRpVo.setGlobalMessageType(NewDetailParamsRpVo.ORTHER_MES_SMS_PURCHASE);
                }
            }
        }
    }
}
